;
(function() {
  new Vue({
    el: '#privilege',
    data: {
      pid: 0,
      index: 0,
      memberType: 'A',
      memberData: [],
      odata: [],
      adata: [],
      csid: null,
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      level: 0,
      platform: 'MHYD',
      tabList: [{
        key: 'admin',
        name: '管理员权限',
        power: 'c228'
      }, {
        key: 'member',
        name: '成员权限',
        power: 'c229'
      }],
      tabIndex: null,
      userType: null, //管理员还是成员
      butList: [{
        key: 'global',
        name: '全局默认权限'
      }, {
        key: 'cutomer',
        name: '客户管理员权限'
      }],
      butIndex: '',
      res_code: 0, //资源id,
      isPriLayer: false, //授权弹出层
      defaultOrg: null, //默认第一个组织
      privileges: [], //权限列表(管理员、成员)
      defprivileges: [], //权限列表（默认，客户管理员）
      orgprivileges: [], //组织拥有的权限
      defaultAndcustomer: [], //全局和客户管理员权限
      functype: 'switchs',
    },
    computed: {
      treeList() {
        let arr = [];
        let parent, children;
        parent = this.adata
        children = this.adata
        for (let i = parent.length - 1; i >= 0; i--) {
          let item0 = parent[i];
          for (let j = 0; j < children.length; j++) {
            let item1 = children[j];
            if (item0.id == item1.parentId) {
              item0.children.push(item1)
            }
          }
        }
        for (let k = 0; k < parent.length; k++) {
          if (Number(parent[k].parentId) > Number(this.csid)) {
            parent.splice(k, 1)
            k--
          }
        }
        arr = parent
        let count = 0;
        guierzi(arr, count)
        return arr
      }
    },
    methods: {
      //获取身份信息
      getStwxdd() {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (res) => {
            if (!res.subOrgs.length) return;
            res.subOrgs.some(item => {
              if (item.orgType == 'DD') {
                res.subOrgs = res.subOrgs.sort(hlArrayUtil.compareAvg('index'))
                this.pid = item.id
                this.csid = item.id
                this.memberData = res.subOrgs;
                this.getStwxddData();
              }
            })
          }
        })

      },
      //根据身份信息id调用
      getStwxddData(fn) {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (data) => {
            if (!data.subOrgs.length) {
              return
            }
            if (this.defaultOrg == null) {
              this.defaultOrg = data.subOrgs[0]
              this.getOrgPrivilege();
            }
            data.subOrgs.map(item => {
              item.flag = true;
              item.oflag = false;
              item.parentId = this.pid;
              item.children = [];
              this.odata.push(item)
            })
            this.odata = hlArrayUtil.removeRepeat(this.odata, 'id');
            this.adata = JSON.parse(JSON.stringify(this.odata))
            if (fn) fn();
          }
        })
      },
      getPidData(id, type, localId) {
        if (type && localId) {
          for (var i = 0; i < this.odata.length; i++) {
            if (this.odata[i].id == localId) {
              this.odata.splice(i, 1)
            }
          }
        }
        this.pid = id;
        this.getStwxddData()
      },
      //响应子组件的方法
      getdata(data) {
        this.defaultOrg = data
        this.getOrgPrivilege();
      },

      //切换tab
      selectTab(index, item) {
        this.tabIndex = index
        this.userType = item.key
        if (this.defaultOrg != null) {
          this.getOrgPrivilege();
        }
      },
      //切换but
      selectbut(index, item) {
        this.butIndex = index
        if (item.key == 'cutomer') {
          this.cutomer_admin_privileges();
        } else {
          this.globle_privileges();
        }
      },
      switchpri(id, code) {
        if (this.functype == 'switchs') {
          this.switchs(id, code)
        } else {
          this.switchss(id, code)
        }
      },

      // 开关切换(管理员、成员)
      switchs(id, code) {
        let data = {
          position_id: '',
          privilege_code: id,
          opt: '',
          platform: this.platform
        }
        if (this.userType == 'admin') {
          data.position_id = this.defaultOrg.adminOrgId
        } else {
          data.position_id = this.defaultOrg.memberOrgId
        }
        if (this.orgprivileges.indexOf(id) == -1) {
          data.opt = 'Y'
        } else {
          data.opt = 'N'
        }
        this.assign_privilege_to_position(data)
      },
      // 开关切换(客户管理员、全局)
      switchss(id) {
        let data = {
          privilege_code: id,
          opt: '',
          platform: this.platform
        }
        if (this.defaultAndcustomer.indexOf(id) == -1) {
          data.opt = 'Y'
        } else {
          data.opt = 'N'
        }

        if (this.butIndex == 0) {
          this.globle_privilege(data)
        } else {
          this.cutomer_admin_privilege(data)
        }
      },
      // 全局默认权限设置
      globle_privilege(data) {
        hlhttp.put({
          url: 'auth/cend/globle_privilege',
          data: data,
          success: (data) => {
            this.globle_privileges();
          }
        })
      },
      // 客户管理员权限设置
      cutomer_admin_privilege(data) {
        hlhttp.put({
          url: 'auth/cend/customer_admin_privilege',
          data: data,
          success: (data) => {
            this.cutomer_admin_privileges();
          }
        })
      },
      // 赋予权限到岗位
      assign_privilege_to_position(data) {
        hlhttp.put({
          url: 'auth/cend/assign_privilege_to_position',
          data: data,
          success: (data) => {
            this.getOrgPrivilege();
          }
        })
      },
      // 获取资源的权限列表（所有权限列表）
      getPrivilege() {
        hlhttp.get({
          url: 'cend/res/res,privileges',
          data: {
            res_code: this.res_code,
            platform: this.platform,
          },
          success: (data) => {
            data.forEach(item => {
              item.children = []
              item.showChild = false
            })
            this.privileges = data
          }
        })
      },
      //获取子资源的权限列表
      getchildPri(item, has) {
        if (has == 'Y') {
          hlhttp.get({
            url: 'cend/res/res,privileges',
            data: {
              res_code: item.code,
              platform: this.platform,
            },
            success: (data) => {
              data.forEach(item => {
                item.children = []
                item.showChild = false
              })
              item.children = data
            }
          })
        }
      },
      //获取组织的权限列表（默认第一个）
      getOrgPrivilege() {
        if (this.userType == null) {
          return
        }
        let id = ''
        if (this.userType == 'admin') {
          id = this.defaultOrg.adminOrgId
        }
        if (this.userType == 'member') {
          id = this.defaultOrg.memberOrgId
        }
        hlhttp.get({
          url: 'position/' + id + '/privileges',
          data: {
            platform: this.platform,
          },
          success: (data) => {
            this.orgprivileges = data
          }
        })
      },
      //授权弹出框
      showLayer(type) {
        this.isPriLayer = true
        this.functype = 'switchss'
        this.defprivileges = [].concat(JSON.parse(JSON.stringify(this.privileges)))
        if (type == 'global') {
          this.butIndex = 0
          this.globle_privileges();
        } else {
          this.butIndex = 1
          this.cutomer_admin_privileges();
        }
      },
      //授权弹出框关闭
      closeLayer() {
        this.isPriLayer = false
        this.functype = 'switchs'
      },
      //获取全局默认权限
      globle_privileges() {
        hlhttp.get({
          url: 'auth/cend/globle_privileges',
          data: {
            platform: this.platform,
          },
          success: (data) => {
            this.defaultAndcustomer = data
          }
        })
      },
      //获取客户管理员权限
      cutomer_admin_privileges() {
        hlhttp.get({
          url: 'auth/cend/customer_admin_privileges',
          data: {
            platform: this.platform,
          },
          success: (data) => {
            this.defaultAndcustomer = data
          }
        })
      }
    },
    mounted() {
      this.getPrivilege();
      this.getStwxdd();
      if (this.permissions.indexOf('c228') == -1) {
        if (this.permissions.indexOf('c229') == -1) {

        } else {
          this.userType = 'member'
          this.tabIndex = 1
        }
      } else {
        this.userType = 'admin'
        this.tabIndex = 0
      }
    }
  })

  function guierzi(arr, count) {
    count++
    arr.map(item => {
      item.level = count
      if (item.children.length) {
        guierzi(item.children, count)
      }
    })
  }
}())
